#include "plum_api.h"
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include "hardware/flow_meter/flow_meter.h"
#include "hardware/press_sensor/press_sensor.h"
#include "hardware/vfd/Simphoenix/dx100.h"
#include "hardware/relay/relay.h"
#include "thirdparty/modbus/modbus.h"
#include "hardware/vfd/vfd.h"

#define LOG_RUN_LEVEL   LOG_LEVEL_DEBUG
#define LOG_MODULE      "main"
#include "thirdparty/log/log.h" 

static const struct option options[] = {
    {"help" ,   1,  NULL,   'h'},
    {"io",      1,  NULL,   'i'},
    {"type",    1,  NULL,   't'},
};

static  int uart_idx; 

void    dev_print_usage(const char *pro_name)
{
    printf("This is a program running simulator device\n");
    printf("Usage: %s \n", pro_name);
    printf("    -h --help       print usage\n");
    printf("    -i --io         serial inedx\n");
    printf("    -t --type       sensor type[0.all,1.flow meter,2.press sensor]\n");
}

int dev_uart_flow_meter_read(unsigned char *data,unsigned int len,unsigned int timeout)
{
    return plum_hal_uart_recv(PLUM_HAL_UART_ID(uart_idx),data,len,timeout);
}

int dev_uart_flow_meter_write(unsigned char *data,unsigned int len)
{
    return plum_hal_uart_send(PLUM_HAL_UART_ID(uart_idx),data,len);
}

int main(int argc,char **argv)
{
    int rc = 0;
    int type = 0;
    int opt;
    while((opt = getopt_long(argc,argv,"h:i:t:",options,NULL)) != -1){
        switch(opt){
            case 'i':   
                {
                    LOG_I("get io:%s",optarg);

                    plum_hal_uart_cof_t uart_cof;
                    uart_cof.databit = PLUM_HAL_UART_DATABIT_8;
                    uart_cof.parity  = PLUM_HAL_UART_PARITY_NONE;
                    uart_cof.stopbit = PLUM_HAL_UART_STOPBIT_1;
                    uart_cof.flowctrl= PLUM_HAL_UART_FLOW_CTRL_NONE;
                    uart_cof.baudrate= 9600;

                    uart_idx = strtoul(optarg,NULL,10);
                    rc = plum_hal_uart_init(PLUM_HAL_UART_ID(uart_idx),&uart_cof);
                    if(rc){
                        LOG_E("plum_hal_uart_init err,rc:%d",rc);
                    }
                }
                break;

            case 't':
                {
                    LOG_I("sensor type: %s",optarg);

                    type = strtoul(optarg,NULL,10);
                }
                break;

            case 'h':
            default:
                dev_print_usage(argv[0]);
                break;
        }
    }

    if(type == 1)
    {
        rc = dev_flow_meter_init(dev_uart_flow_meter_read,dev_uart_flow_meter_write);
        if(rc){
            LOG_E("dev_flow_meter_init err,rc:%d",rc);
        }

        float data; 
        while(1){
            data = dev_flow_meter_anappy_read(1);
            if(data < 0){
                LOG_E("dev_flow_meter_anappy_read err,rc:%0.2f",data);
            }
            else{
                LOG_I("瞬时流量：%f (m³/s)",data);
            }

            sleep(1);

            data = dev_flow_meter_total_read(1);
            if(data < 0){
                LOG_E("dev_flow_meter_total_read err,rc:%0.2f",data);
            }
            else{
                LOG_I("累计流量：%f (m³)",data);
            }

            sleep(1);
        }
    }
    else if(type == 2)
    {
        rc = dev_press_sensor_init(dev_uart_flow_meter_read,dev_uart_flow_meter_write);
        if(rc){
            LOG_E("dev_press_sensor_init err,rc:%d");
        }

        float data; 
        while(1){
            data = dev_press_sensor_read(2);
            if(data < 0){
                LOG_E("press sensor read data err,rc:%0.1f",data);
            }
            else{
                LOG_I("压力: %f (kPa)",data);
            }

            sleep(1);
        }
    }
    else if(type == 0){
        rc = dev_flow_meter_init(dev_uart_flow_meter_read,dev_uart_flow_meter_write);
        if(rc){
            LOG_E("dev_flow_meter_init err,rc:%d",rc);
        }

        rc = dev_press_sensor_init(dev_uart_flow_meter_read,dev_uart_flow_meter_write);
        if(rc){
            LOG_E("dev_press_sensor_init err,rc:%d");
        }

        float data;
        while(1){
            data = dev_press_sensor_read(2);
            if(data < 0){
                LOG_E("press sensor read data err,rc:%0.1f",data);
            }
            else{
                LOG_I("压力     : %f (kPa)",data);
            }
            sleep(1);

            data = dev_flow_meter_anappy_read(1);
            if(data < 0){
                LOG_E("dev_flow_meter_anappy_read err,rc:%0.2f",data);
            }
            else{
                LOG_I("瞬时流量 ：%f (m³/s)",data);
            }

            sleep(1);

            data = dev_flow_meter_total_read(1);
            if(data < 0){
                LOG_E("dev_flow_meter_total_read err,rc:%0.2f",data);
            }
            else{
                LOG_I("累计流量 ：%f (m³)",data);
            }

            sleep(1);
        }
    }
    else if(type == 3){
#if 0
        rc = simphoenix_dx100_init(dev_uart_flow_meter_read,dev_uart_flow_meter_write);
        if(rc){
            LOG_E("simphoenix_dx100_init err,rc:%d",rc);
        }
    
        LOG_I("运行变频器");
        while(1){
            rc = simphoenix_dx100_run();
            if(rc){
                LOG_E("simphoenix_dx100_run err,rc:%d",rc);
            }

            sleep(1);

            simphoenix_dx100_fre(5000);
        }
#else
        LOG_I("运行变频器");
        while(1){
            rc = dev_vfd_run(DEV_VFD_RUN_PST);
            if(rc){
                LOG_E("dev_vfd_run err,rc: %d",rc);
            }

            sleep(1);
            rc = dev_vfd_fre(5000);
            if(rc){
                LOG_E("dev_vfd_fre err,rc: %d",rc);
            }
        }
#endif
    }
    else {
        rc = plum_cal_cli_init(getchar);
        if(rc){
            LOG_E("init cli err,rc:%d",rc);
        }

        rc = modbus_protocol_register_io(dev_uart_flow_meter_read,
                                    dev_uart_flow_meter_write);
        if(rc){
            LOG_E("init modbus err,rc:%d",rc);
        }

        while(1){
            sleep(1);
        }
    }


    return (0);
}

/*****************************************************************************
 * Cli
 ****************************************************************************/

plum_s32    dev_cli_relay_switch(plum_u32 argc,plum_s8 **argv)
{
    if(argc != 2){
        LOG_E("parameter err");
        return -1;
    }

    plum_u8    relay_idx = strtoul(argv[0],NULL,10);
    LOG_I("relay index : %d",relay_idx);

    plum_u8     relay_sta = strtoul(argv[1],NULL,10);
    LOG_I("relay state : %d",relay_sta);

    if(relay_sta){
        return dev_relay_on(relay_idx);
    }
    else{
        return dev_relay_off(relay_idx);
    }   
}
PLUM_CAL_CLI_EXPORT("relay_ctl" ,
                    "control relay on/off,eg,relay_ctl 1 1",
                    dev_cli_relay_switch);

plum_s32    dev_cli_flow_meter_read_anappy(plum_u32 argc,plum_s8 **argv)
{
    if(argc != 0){
        return -1;
    }

    PLUM_UNUSED(argc);
    PLUM_UNUSED(argv);

    float data = dev_flow_meter_anappy_read(1);
    if(data < 0){
        LOG_E("read anappy data err,rc:%d",data);
    }
    LOG_I("read anappy : %f (m³/s)",data);

    return 0;
}
PLUM_CAL_CLI_EXPORT("anappy_read",
                    "read flow sensor for anappy,eg.anappy_read",
                    dev_cli_flow_meter_read_anappy);
